<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**--tmpl-use--**/

use herosphp\core\Loader;


class BrandAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();
	}

	protected $serviceBean="information.brand.service";
	protected $product = "product.product.service";
	public function add(HttpRequest $request) {
		parent::add($request);
		//特约品牌类型
		$cates = Beans::get($this->product)->field("DISTINCT type_name")->find();
		$this->assign('cates',$cates);
		$this->assign("title", "添加特约品牌");
        $this->assign('bread', array('信息管理','特约品牌列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/brand/index'),'javascript:;'));
	
	}
	public function edit(HttpRequest $request) {
		//特约品牌类型
		$cates = Beans::get($this->product)->field("DISTINCT type_name")->find();
		$this->assign('cates',$cates);
		parent::edit($request);
		$this->assign("title", "编辑特约品牌");
        $this->assign('bread', array('信息管理','特约品牌列表',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;',url('admin/brand/index'),'javascript:;'));
	}
	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','特约品牌');
		$s_title = $request->getParameter('s_title');
		$this->assign('s_title',$s_title);
		if($s_title !== '' && $s_title!== NULL)$conditions['title'] = '%'.$s_title.'%';

		$s_addtime = $request->getParameter('s_addtime');
		$this->assign('s_addtime',$s_addtime);
		if($s_addtime !== '' && $s_addtime!== NULL)$conditions['addtime'] = '%'.$s_addtime.'%';



		$s_cate = $request->getParameter('s_cate');
		$this->assign('s_cate',$s_cate);
		if($s_cate !== '' && $s_cate!== NULL)$conditions['cate'] = '%'.$s_cate.'%';


		$s_s_addtime = $request->getParameter('s_s_addtime');
		$s_e_addtime = $request->getParameter('s_e_addtime');
		$this->assign('s_s_addtime',$s_s_addtime);
		$this->assign('s_e_addtime',$s_e_addtime);

		if($s_s_addtime !== '' && $s_s_addtime!== NULL){
			$conditions['addtime']['>'] = $s_s_addtime.' 00:00:00';
		}

		if($s_e_addtime !== '' && $s_s_addtime!== NULL){
			if(!$conditions['addtime']){ $conditions['addtime'] = array();}
			$conditions['addtime']['<='] = $s_e_addtime.' 23:59:59';
		}

		$s_status = $request->getParameter('s_status');
		$this->assign('s_status',$s_status);
		if($s_status !== '' && $s_status!== NULL)$conditions['status'] = $s_status;

		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->assign('_filter',array('title','cate','addtime','url','logo','listorder','app_id'));
		$this->assign('_header',array('标题','分类','添加时间','网址','LOGO','排序','appid'));
		$this->setConditions($conditions);

		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);

		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		$service = Beans::get($this->getServiceBean());
		$total = $service->count($this->getConditions());
		$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
				array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
		//初始化分页类
		$this->generatePageMenu($total);
		$this->assign('total',$total);

		$cates = Beans::get($this->getServiceBean())->field("DISTINCT cate")->find();
		$this->assign('cates',$cates);
	
		$this->assign("title", "特约品牌列表");
        $this->assign('bread', array('信息管理',$this->getTemplateVar('title')));
        $this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->assign('pagesize', $this->getPagesize());
		$this->assign('items', $items);
		$this->setView(strtolower($request->getAction()) . '/index');
	}
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		if(!$data['status']){$data['status'] = 0;}
		$data['addtime'] = date("Y-m-d H:i:s");
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		if(!$data['status']){$data['status'] = 0;}
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}
	
	public function setStatus(HttpRequest $request)
	{
		$status = $request->getParameter("status");
		if( $status=="" ){
			AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,"操作失败",null);
		}
		$ids = $request->getParameter('ids');
		if ( empty($ids) ) AjaxResult::ajaxResult('error', COM_ERR_MSG);

		$service = Beans::get($this->getServiceBean());
		if( $status == 1 ){
			$service->updates(array('status'=>1),array('id' => array('$in' => $ids)));
			AjaxResult::ajaxSuccessResult();
		}else{
			$service->updates(array('status'=>0),array('id' => array('$in' => $ids)));
			AjaxResult::ajaxSuccessResult();
		}
	}

}
